package com.scalar.chiptrack.finance.openpo.actioncommands;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntry;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntryList;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderReceiptForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;

public class PurchaseOrderReceiptLoadLotPartNumbersCommand extends ChipTrackExtendedAction
{
	public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
	{
		HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String actionForward 		= "openRecevingLineItemLotEntry";
		HttpSession mSession 		= request.getSession();

		PurchaseOrderReceiptForm receiptForm = ( PurchaseOrderReceiptForm ) form;
        int receivingLotIndex = -1;
        try
        {
            receivingLotIndex = Integer.parseInt ( request.getParameter( "recevingLotIndex" ) );
        }catch ( NumberFormatException e ) {}
        if ( receivingLotIndex >= 0 )
        {
            try
            {
                PurchaseOrderLineItemLotDataEntryList openPOLotList = receiptForm.getOpenPOLotList();
                PurchaseOrderLineItemLotDataEntry purchaseOrderLineItemLotDataEntry = (PurchaseOrderLineItemLotDataEntry)openPOLotList.get( receivingLotIndex );
                ArrayList partNumberList = PurchaseOrderDAO.loadPartNumbersByVendorIdAndLocation( receiptForm.getVendor_id(), purchaseOrderLineItemLotDataEntry.getPsTypeName() );
                purchaseOrderLineItemLotDataEntry.setPartNumbersList( partNumberList );
            }
            catch ( Exception e )
            {
                e.printStackTrace();
                Logger.error( ERRROR_LOADING_PURCHASE_ORDER_LINE_ITEM_PURCHASING_LOT_DATA_ENTRY, e );
                errors.put( ERROR_INTERNAL_SERVER_ERROR, ERRROR_LOADING_PURCHASE_ORDER_LINE_ITEM_PURCHASING_LOT_DATA_ENTRY );
                mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( ERRROR_LOADING_PURCHASE_ORDER_LINE_ITEM_PURCHASING_LOT_DATA_ENTRY, e ) );
            }
        }
        request.setAttribute( "processNameList", PurchaseOrderDAO.loadProcessNames( receiptForm.getVendor_id() ) );
        actionForward = "openRecevingLineItemLotEntry";


		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
	}
}
